const { default: BigNumber } = require('bignumber.js');

module.exports = class extends think.Model {
  async index(lon, lat, page, size) {
    const lonNum = parseFloat(lat);
    const latNum = parseFloat(lon);
    // const d = await this.query(`
    //   SELECT
    //   *
    //   FROM tour_view_list
    //   WHERE (ACOS(SIN((${latNum}*3.1415)/180) * SIN((lat*3.1415)/180) + COS((${latNum}*3.1415)/180) * COS((lat*3.1415)/180) * COS((${lonNum}*3.1415)/180 - (lon*3.1415)/180))*6370.996)<=100000
    // `);
    const start = (page - 1) * size;
    const end = (page - 1) * size + size;
    const d = await this.query(`
    SELECT *,
    (
      6371 * acos(
        cos(radians(${lonNum}))
        * cos( radians( lat ) )
        * cos(radians( lon ) - radians(${latNum}))
        +
        sin( radians(${lonNum}) )
        * sin( radians( lat ) )
      )
    )
    AS distance

    FROM job_post_list
    WHERE del = 0
    HAVING distance < 10000
    ORDER BY distance
    LIMIT ${start}, ${end}  
    `);
    return d;
  }
};
